RealDataGrid = {};
RealDataGrid.selectVehicleIds="";//需要刷新的车辆id
//表格当前选中的实时数据的车辆Id及车牌号
RealDataGrid.selectedVehicleId = 0;
RealDataGrid.selectedPlateNo = '';
RealDataGrid.realDataList = [];//实时数据加载到内存中，进行内存分页
RealDataGrid.rowIndexMap = {};//根据实时数据ID,找到内存数组realDataList的索引
//RealDataGrid.realDataMap = {};//根据实时数据ID,找到内存数组realDataList的索引
RealDataGrid.update = false;
RealDataGrid.refreshInterval = 10;
RealDataGrid.gridId = "realDataGrid";


RealDataGrid.create = function(refreshInterval)
{
    this.refreshInterval = refreshInterval || 10;//刷新间隔,确定定时器的间隔
    this.update = false;
    var treeUrl = globalConfig.webPath+"/vehicle/refreshRealData.action";
//绑定数据列表 //align: 'left' 控制对齐方式
    this.realDataGrid=	$("#realDataGrid");

    this.timerName = "realdataTimer";
    var strInterval = this.refreshInterval + 's';
    var me = this;
    $('body').everyTime(strInterval, this.timerName,function(){
        //do something...
        me.refresh(true);//更新模式
    });

    return this.realDataGrid;

}
//点击数据表格的行时，显示地图车辆
RealDataGrid.onClickRow = function(rowIndex, rowData)
{
	
	var plateNo = rowData.plateNo;
	var vehicleId = rowData.vehicleId;
	MyMap.showVehicleOnMap(vehicleId);
	RealDataGrid.selectedVehicleId = vehicleId;
	RealDataGrid.selectedPlateNo = plateNo;
	$("#realDataPlateNo").val(plateNo);
}
/**
 * 当用户点击车辆树的时候，设置车牌号和Id
 */
RealDataGrid.setPlateNo = function(plateNo, vehicleId)
{
	$("#realDataPlateNo").val(plateNo);
	this.selectedPlateNo = plateNo;
	this.selectedVehicleId = vehicleId;
}

//将上线状态转换成图标显示
function formatOnlineState(val,row){
	        
			if (val){
				var onlineImage = globalConfig.webPath+"/image/power_on.png";
				return '<img src="'+onlineImage + '" />';
			} else {
				var offlineImage = globalConfig.webPath+"/image/power_off.gif";
				return '<img src="'+offlineImage + '" />';
			}
		}
/**
 * 清除表格数据
 */
RealDataGrid.clear = function()
{	
	$("#"+ this.gridId ).empty();
	this.realDataList = [];
	this.rowIndexMap = {};
}

function safeString(str)
{
  return str == null ? "" : str;
}
/**
* 定时发送ajax请求，更新表格,
* 如有表格中有对应的车辆就更新
* 如果没有该车辆，就增加一套
*/
RealDataGrid.refresh = function(update)
{
	//update = false;
	if(!this.selectVehicleIds || this.selectVehicleIds.length == 0)
		return;
	if(update == false)
	{
		//jQuery('#'+this.gridId).showLoading(); 
		$("#realDataGridTips").show();
	}
	var me = this;
	var url = globalConfig.webPath+"/realData/refresh.action";
	var params = {update:update};	 
     $.ajax({
            type: "POST",
            url: url,
			data:params,
			error:function(){
			   //alert("网络连接错误，无法读取数据!");
               //Utility.done();
			},
            success: function(result){
				//var gridData = {Rows:data};
				var records = result.data;
				if(records == null)
					return;
	            var start = new Date();
				if(update == false)
				{
					MyMap.clearAllVehicle();
				    MyMap.showVehicleListOnMap(records,true);
					$("#"+ me.gridId ).empty();
					me.rowIndexMap = {};
					//me.realDataMap = {};
					//第一次全部加载
					me.realDataList = records;//保存在内存中，进行分页
					var newRow = "";
					for(var m = 0; m < records.length; m++)
					{
						
						var r = records[m];
						console.log("需要格式化的实时数据：",r);
						me.rowIndexMap[r.vehicleId] = m;
						var trClass= m % 2 == 0 ? "" : "datagrid-row-odd";
						if(m == 0)
						{
							//修改代码重新排序
							newRow +="<tr id=rd_" + m + "  class=" + trClass + "><td style='width:20px'>" + (m + 1) + "</td>"//id+序列号

									+"<td style='width:70px'>" + r.plateNo + "</td><td style='width:80px'>" + r.simNo + "</td>"//车牌号+通讯号
									+"<td style='width:120px'>" + safeString(r.updateDate) + "</td><td style='width:110px'>" + safeString(r.sendTime) + "</td>"//上报时间+gps定位时间
									+"<td  style='width:80px'>" + safeString(r.velocityDescr) + "</td><td style='width:90px'>" + safeString(r.recordVelocity) + "</td>"//gps速度+记录仪速度
									+"<td style='width:40px'>" + formatOnlineState(r.online) + "</td><td style='width:80px'>" + r.status + "</td>"//车辆状态+报警状态
									+"<td style='width:220px'>" + r.location + "</td><td style='width:90px'>" + safeString(r.longitude) + "</td>"//位置+经度
									+"<td style='width:90px'>" + safeString(r.latitude) + "</td>"//纬度


									// +"<td style='width:80px'>" + r.plateNo + "</td><td style='width:120px'>" + safeString(r.sendTime) + "</td>"//车牌号+gps定位时间
									// +"<td style='width:40px'>" + formatOnlineState(r.online) + "</td><td style='width:80px'>" + r.status + "</td>"//在线+状态
									// +"<td style='width:40px'>" + safeString(r.velocityDescr) + "</td><td style='width:240px' title='" + r.location + "'>" + r.location + "</td>"//速度+位置
									// +"<td style='width:60px'>" + safeString(r.todayMileage) + "</td><td style='width:60px'>" + safeString(r.mileage) + "</td>"//当天里程+累计里程
									// +"<td  style='width:60px'>" + safeString(r.recordVelocity) + "</td><td style='width:80px'>" + safeString(r.directionDescr) + "</td>"//脉冲速度+方向
									// +"<td  style='width:60px'>" + safeString(r.gas) + "</td><td style='width:40px'>" + safeString(r.valid) + "</td>"//油量+定位
									// +"<td style='width:40px'>" +  safeString(r.altitude) + "</td><td style='width:90px'>" + safeString(r.longitude) + "</td>"//海拔+经度
									// +"<td style='width:90px'>" + safeString(r.latitude) + "</td><td style='width:120px'>" + r.depName + "</td>"//纬度+所属分组
									// +"<td style='width:120px'>" + r.simNo + "</td><td style='width:40px'>" + r.plateColor + "</td>"//当天里程+累计里程

									+"</tr>";//行结尾


							/*
							newRow += "<tr id=rd_" + m + "  class=" + trClass + "><td style='width:20px'>" + (m + 1) + "</td><td style='width:80px'>" + r.plateNo + "</td><td style='width:120px'>" + r.depName + "</td><td style='width:120px'>"
								+ r.simNo + "</td><td style='width:40px'>" + r.plateColor + "</td><td style='width:40px'>" + formatOnlineState(r.online) + "</td><td style='width:120px'>"
								+ safeString(r.sendTime) + "</td><td style='width:240px' title='" + r.location + "'>" + r.location + "</td><td style='width:80px'>" + r.status + "</td><td style='width:40px'>"
								+ safeString(r.velocityDescr) + "</td><td  style='width:60px'>" + safeString(r.recordVelocity) + "</td><td style='width:80px'>" + safeString(r.directionDescr) + "</td><td  style='width:60px'>"
								+ safeString(r.todayMileage) + "</td><td  style='width:60px'>"
								+ safeString(r.mileage) + "</td><td  style='width:60px'>" + safeString(r.gas) + "</td><td style='width:40px'>" + safeString(r.valid) + "</td><td style='width:40px'>"
								+ safeString(r.altitude) + "</td><td style='width:90px'>" + safeString(r.longitude) + "</td><td style='width:90px'>" + safeString(r.latitude) + "</td><td style='width:20px'>"
								+ r.vehicleId + "</td></tr>";*/
						}else
						{
							//修改代码重新排序
							newRow += "<tr id=rd_" + m + "  class=" + trClass + "><td >" + (m + 1) + "</td>"//id+序列号

								+"<td>" + r.plateNo + "</td><td>" + r.simNo + "</td>"//车牌号+通讯号
								+"<td>" + safeString(r.updateDate) + "</td><td>" + safeString(r.sendTime) + "</td>"//上报时间+gps定位时间
								+"<td>" + safeString(r.velocityDescr) + "</td><td>" + safeString(r.recordVelocity) + "</td>"//gps速度+记录仪速度
								+"<td>" + formatOnlineState(r.online) + "</td><td>" + r.status + "</td>"//车辆状态+报警状态
								+"<td>" + r.location + "</td><td >" + safeString(r.longitude) + "</td>"//位置+经度
								+"<td>" + safeString(r.latitude) + "</td>"//纬度

								// + "<td >" + r.plateNo + "</td><td >" + safeString(r.sendTime) + "</td>"//车牌号+gps定位时间
								// + "<td >" + formatOnlineState(r.online) + "</td><td >" + r.status + "</td>"//在线+状态
								// + "<td >" + safeString(r.velocityDescr) + "</td><td title='" + r.location + "'>" + r.location + "</td>"//速度+位置
								// + "<td >" + safeString(r.todayMileage) + "</td><td >" + safeString(r.mileage) + "</td>"//当天里程+累计里程
								// + "<td >" + safeString(r.recordVelocity) + "</td><td >" + safeString(r.directionDescr) + "</td>"//脉冲速度+方向
								// + "<td >" + safeString(r.gas) + "</td><td >" + safeString(r.valid) + "</td>"//油量+定位
								// + "<td >" + safeString(r.altitude) + "</td><td >" + safeString(r.longitude) + "</td>"//海拔+经度
								// + "<td >" + safeString(r.latitude) + "</td><td >" + r.depName + "</td>"//纬度+所属分组
								// + "<td >" + r.simNo + "</td><td >" + r.plateColor + "</td>"//当天里程+累计里程

								+ "</tr>";//行结尾

							/*
							newRow += "<tr id=rd_"+m+"  class="+trClass+"><td >" + (m+1) + "</td><td >"+ r.plateNo + "</td><td >"+ r.depName + "</td><td >"
								 + r.simNo + "</td><td >"+ r.plateColor + "</td><td >"+ formatOnlineState(r.online) + "</td><td '>" 
								  +safeString( r.sendTime) + "</td><td title='"+ r.location + "'>"+ r.location + "</td><td >"+ r.status + "</td><td >"
								  + safeString(r.velocityDescr) + "</td><td  >"+ safeString(r.recordVelocity) + "</td><td >"+ safeString(r.directionDescr) + "</td><td  >"
								  + safeString(r.todayMileage ) + "</td><td  >"
								  + safeString(r.mileage )+ "</td><td  >"+ safeString(r.gas) + "</td><td >"+ safeString(r.valid) + "</td><td >"
								  + safeString(r.altitude) + "</td><td '>"+safeString( r.longitude) + "</td><td >"+ safeString(r.latitude) + "</td><td '>" 
								  + r.vehicleId + "</td></tr>";*/
						}
					}
				    $("#"+ me.gridId ).append(newRow);
					
		            $("#realDataGridTips").hide();
		            //jQuery('#'+me.gridId).hideLoading(); 
					$("#"+ me.gridId + " tr").hover(function () {  						    
							var selectedIndex = $(this).attr("class").indexOf("datagrid-row-selected");
							if(selectedIndex < 0)
						   {
								//$(this).css({ "background-color": "#87CEEB" });  
								$(this).addClass("datagrid-row-over");
								$(this).removeClass("datagrid-row-odd");
						   }
						},function () {  
							var selectedIndex = $(this).attr("class").indexOf("datagrid-row-selected");
							if(selectedIndex < 0)
						   {
								$(this).removeClass("datagrid-row-over");
								var $index = $(this).index();  
								if ($index % 2 == 0) {  
									//$(this).css("datagrid-row-over");  								
								   $(this).addClass("datagrid-row-odd");
								} else {  							
								   $(this).removeClass("datagrid-row-odd");
									//$(this).css({ "background-color": "#E4F5FF" });  
								}  
						   }
						}).click(function(){
							//勾选车辆树上的车辆，在实时数据上添加车辆数据，点击数据行后，调用本方法 author :shinion date:2019-3-18
							// console.log("开始点击实时数据的数据行222222。。。，，，，");
							var s=$(this).attr("class");
							$("#"+ me.gridId + " tr").removeClass("datagrid-row-selected");
							var rowIndex = $(this).index(); 
							var rowData = me.realDataList[rowIndex];
							var plateNo = rowData.plateNo;
							var vehicleId = rowData.vehicleId;
							MyMap.showVehicleOnMap(vehicleId);
							RealDataGrid.selectedVehicleId = vehicleId;
							RealDataGrid.selectedPlateNo = plateNo;
							$("#realDataPlateNo").val(plateNo);
							if(s!=null&&s!='undefined'){
								var ofright = $(this).attr("class").indexOf("datagrid-row-selected");
								if(ofright>-1){
									$(this).removeClass("datagrid-row-selected");
								}else{
									$(this).addClass("datagrid-row-selected");
									$(this).removeClass("datagrid-row-odd");
								   $(this).removeClass("datagrid-row-over");
								}
							}else{
								$(this).addClass("datagrid-row-selected");
								$(this).removeClass("datagrid-row-odd");
								$(this).removeClass("datagrid-row-over");
							}
						});  

				}else
				{
					//没有要更新的实时数据
					if(records.length == 0)
						return;
					
					//第一次全部加载后，转入更新模式，即只从后台查询到有更新的实时数据，减少流量
					//var trList = $("#"+ me.gridId).children();

					for(var m = 0; m < records.length; m++)
					{
						var r = records[m];

						var index = me.rowIndexMap[r.vehicleId];
						if(index != null)
						{
						   me.realDataList[index] = r;//更新内存记录					   
							
						}

						//$("#"+ me.gridId + " tr:eq("+index + ") td:nth-child(1)").html("value");   
						//var trObj = $("#"+ me.gridId + " tr:eq("+index + ")");
						//var trObj = trList[index];
						//trObj = $(trObj);
						var tdList = $("#rd_"+index).children();

						//更新数据，更新字段排列顺序
						//修改代码
						var dataArray = [r.plateNo,r.simNo,r.updateDate,r.sendTime,r.velocityDescr,r.recordVelocity,
							formatOnlineState(r.online) ,r.status, r.location,r.longitude,r.latitude];
							/*
						var dataArray = [r.plateNo,r.depName,r.simNo ,r.plateColor ,formatOnlineState(r.online) ,r.sendTime, r.location,r.status,r.velocityDescr,
							r.recordVelocity,r.directionDescr,r.todayMileage,r.mileage,r.gas,r.valid,r.altitude, r.longitude,r.latitude,r.vehicleId ];*/
						for(var k = 1; k < tdList.length; k++)
						{
						    var td = tdList[k];
							var tdValue = dataArray[k-1];
							if(td.innerHTML != tdValue)
							{
						       td.innerHTML = tdValue;
							   td.title = tdValue;
							}
						}
					}	
					var onlyUpdate = true;
					MyMap.showVehicleListOnMap(records,false,onlyUpdate);
				}
				/**
				var selectedRow = me.realDataGrid.datagrid("getSelected");
				if(selectedRow!=null && update )
				{
				   var plateNo = selectedRow.plateNo;
				   var index = me.realDataGrid.datagrid('getRowIndex',plateNo);
				   me.realDataGrid.datagrid('selectRow',index);
				}*/

				
				var end = new Date();
	            var interval = 0.001 * (end - start) ;

	            //if(console)
	               //console.log("载入"+records.length+"条数,耗时:"+ interval+"秒");

				


				/**
				if(data.success == false && data.message){
					alert("操作发生错误! 错误原因:"  + data.Message);

				}*/
              
                
            }
        });


}
//将上线状态转换成图标显示
function formatOnlineState(val,row){
	        
			if (val){
				var onlineImage = globalConfig.webPath+"/image/power_on.png";
				return '<img src="'+onlineImage + '" />';
			} else {
				var offlineImage = globalConfig.webPath+"/image/power_off.gif";
				return '<img src="'+offlineImage + '" />';
			}
		}
						
	 ;


